package com.bothin.experiment4.service.impl;

import com.bothin.experiment4.dao.ProductMapper;
import com.bothin.experiment4.dto.ContactDetailDTO;
import com.bothin.experiment4.form.ContactForm;
import com.bothin.experiment4.model.ContactDetail;
import com.bothin.experiment4.model.Product;
import com.bothin.experiment4.service.IContactDetailService;
import com.bothin.experiment4.service.base.BaseServiceImpl;
import com.bothin.experiment4.utils.KeyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by bothin
 * At 2019/12/17 19:34
 * Description:
 */
@Service
public class ContactDetailServiceImpl extends BaseServiceImpl<ContactDetail> implements IContactDetailService {
    @Override
    public ContactDetail findById(Object id) {
        return contactDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ContactDetail> findAll() {
        return contactDetailMapper.selectAll();
    }

    @Override
    @Transactional
    public void deleteById(Object id) {
        contactDetailMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void update(ContactDetail contactDetail) {
        contactDetailMapper.updateByPrimaryKeySelective(contactDetail);
    }

    @Override
    @Transactional
    public void insert(ContactDetail contactDetail) {
        contactDetailMapper.insertSelective(contactDetail);
    }


    public ContactDetailDTO converter(ContactDetail ContactDetail){
        ContactDetailDTO contactDetailDTO = new ContactDetailDTO();
        BeanUtils.copyProperties(ContactDetail,contactDetailDTO);
        Product product = productMapper.selectByPrimaryKey(contactDetailDTO.getProductId());
        contactDetailDTO.setProduct(product);
        return contactDetailDTO;
    }

    public List<ContactDetailDTO> converter(List<ContactDetail> contactDetails){
        return contactDetails.stream().map(e->converter(e)).collect(Collectors.toList());
    }

    @Override
    public List<ContactDetailDTO> findByContactId(String contactId) {
        Example example = new Example(ContactDetail.class);
        example.createCriteria().andEqualTo("contactId", contactId);
        return converter(contactDetailMapper.selectByExample(example));
    }

    @Override
    public ContactDetailDTO findByContactIdAndProductIdDTO(String contactId, String productId) {
        Example example = new Example(ContactDetail.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("contactId",contactId);
        criteria.andEqualTo("productId",productId);
        List<ContactDetail> contactDetails = contactDetailMapper.selectByExample(example);
        if (contactDetails.size()==1)
            return converter(contactDetails.get(0));
        else
            return null;
    }

    @Override
    public ContactDetail findByContactIdAndProductId(String contactId, String productId) {
        Example example = new Example(ContactDetail.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("contactId",contactId);
        criteria.andEqualTo("productId",productId);
        List<ContactDetail> contactDetails = contactDetailMapper.selectByExample(example);
        if (contactDetails.size()==1)
            return contactDetails.get(0);
        else
            return null;
    }

    @Override
    @Transactional
    public void updateByContactForm(ContactForm contactForm) {
        Map<String,String> map = new HashMap<>();
        for (int i = 0; i < contactForm.getProductId().length; i++) {
            map.put(contactForm.getProductId()[i],"0");
            ContactDetail contactDetail = findByContactIdAndProductId(contactForm.getContactId(), contactForm.getProductId()[i]);
            if (contactDetail==null) {
                //插入新详情
                contactDetail = new ContactDetail(KeyUtil.genUniqueKey(), contactForm.getProductId()[i],
                        contactForm.getContactId(), contactForm.getProductQuantity()[i], contactForm.getProductQuantity()[i]);
                insert(contactDetail);
                return;
            }
            //更新数量
            contactDetail.setProductQuantity(contactForm.getProductQuantity()[i]);
            contactDetail.setPrintStatus(contactForm.getProductQuantity()[i]);
            update(contactDetail);
        }
        //判断哪些详情需要删除
        List<String> productIds = contactDetailMapper.findAllProductIdByContactId(contactForm.getContactId());
        for (String product : productIds) {
            //删除详情
            if (!map.containsKey(product)){
                contactDetailMapper.deleteByProductId(product);
            }
        }
    }

    @Override
    @Transactional
    public void insertByContactForm(ContactForm contactForm) {
        for (int i = 0; i < contactForm.getProductId().length; i++) {
            if (contactForm.getProductQuantity()[i]!=null){
                //插入新详情
                ContactDetail contactDetail = new ContactDetail(KeyUtil.genUniqueKey(), contactForm.getProductId()[i],
                        contactForm.getContactId(), contactForm.getProductQuantity()[i], contactForm.getProductQuantity()[i]);
                insert(contactDetail);
            }
        }
    }

    /**
     * 判断这个合同的所有详情是否已全部生成发货单
     * @param contactId
     * @return
     */
    @Override
    public boolean judgeAllPrintStatusIsZeroById(String contactId) {
        List<Integer> status = contactDetailMapper.findAllPrintStatusByContactId(contactId);
        for (Integer flag : status) {
            //flag为0时，表示这条详情已全部生成发货单
            if (flag!=0)
                return false;
        }
        return true;
    }
}
